const User = require('../models/User');
const { generateToken } = require('../utils/jwt');

// 用户注册
const register = async (ctx) => {
    try {
        const { name, email, password } = ctx.request.body;

        // 检查用户是否已存在
        const existingUser = await User.findOne({ email });
        if (existingUser) {
            ctx.status = 400;
            ctx.body = {
                success: false,
                message: '该邮箱已被注册',
            };
            return;
        }

        // 创建新用户
        const user = await User.create({
            name,
            email,
            password,
        });

        // 生成token
        const token = generateToken({
            id: user._id,
        });

        ctx.status = 201;
        ctx.body = {
            success: true,
            message: '用户注册成功',
            data: {
                user: {
                    id: user._id,
                    name: user.name,
                    email: user.email,
                },
                token,
            },
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: error.message || '服务器内部错误',
        };
    }
};

// 用户登录
const login = async (ctx) => {
    try {
        const { email, password } = ctx.request.body;

        // 检查邮箱和密码是否存在
        if (!email || !password) {
            ctx.status = 400;
            ctx.body = {
                success: false,
                message: '请输入邮箱和密码',
            };
            return;
        }

        // 查找用户
        const user = await User.findOne({ email }).select('+password');
        if (!user) {
            ctx.status = 401;
            ctx.body = {
                success: false,
                message: '邮箱或密码错误',
            };
            return;
        }

        // 验证密码
        const isMatch = await user.comparePassword(password);
        if (!isMatch) {
            ctx.status = 401;
            ctx.body = {
                success: false,
                message: '邮箱或密码错误',
            };
            return;
        }

        // 生成token
        const token = generateToken({
            id: user._id,
        });

        ctx.status = 200;
        ctx.body = {
            success: true,
            message: '登录成功',
            data: {
                user: {
                    id: user._id,
                    name: user.name,
                    email: user.email,
                },
                token,
            },
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: error.message || '服务器内部错误',
        };
    }
};

// 获取当前用户信息
const getMe = async (ctx) => {
    try {
        const user = await User.findById(ctx.state.user.id);

        ctx.status = 200;
        ctx.body = {
            success: true,
            data: {
                user: {
                    id: user._id,
                    name: user.name,
                    email: user.email,
                },
            },
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: error.message || '服务器内部错误',
        };
    }
};

module.exports = {
    register,
    login,
    getMe,
};